project(dmis-tests)
cmake_minimum_required( VERSION 2.8 )
enable_testing()

# run tests via 'mkdir tmp && cd tmp && cmake ../tests && make test' - or use a script

if( UNIX )
  set( dir_separator "/" )
else() #windows
  set( dir_separator "\\" )
endif()
set( exe_path ..${dir_separator}..${dir_separator}bin )
set( pars_test_path ..${dir_separator}..${dir_separator}parserTestFiles )

set( annex_path ${pars_test_path}${dir_separator}annexAIn )
set( ok_path ${pars_test_path}${dir_separator}OkIn )
set( error_path ${pars_test_path}${dir_separator}ErrorIn )

file( GLOB annex_files ${annex_path} *.dmi )
file( GLOB ok_files ${ok_path} *.dmi )
file( GLOB error_files ${error_path} *.dmi )

#-----------------------------------------------------------------------
#functionality of checkLevels
set( checklevels_exe ${exe_path}${dir_separator}dmisConformanceRecorder )

#this macro is called once per .dmi file. It runs checkLevel.cmake, with some variables set.
#the variables are ${EXE} and ${DMI}
macro( levelOk dmi_file )
  get_filename_component( j ${dmi_file} NAME )          # ${dmi_file} includes the relative path
  string(REGEX REPLACE "\..*$" "" k ${j} )              # ${k} is now the filename minus extension
  add_test( NAME ${k}
            COMMAND ${CMAKE_COMMAND}
            -DEXE=${checklevels_exe}
            -DDMI=${dmi_file}
            -P ${CMAKE_CURRENT_SOURCE_DIR}/checkLevel.cmake
          )
endmacro()

foreach( i ${annex_files} )
  levelOk( ${i} )
endforeach( i )

foreach( i ${ok_files} )
  levelOk( ${i} )
endforeach( i )

#-----------------------------------------------------------------------
#functionality of testFullParser

#note - some files handled differently than others in that dir:
#runOut $INA/A.28.dmi              $OUTA/A.28.out   || exit
#runOut $INO/confusing.dmi         $OUTO/confusing.out || exit
set( files_not_clean "${annex_path}${dir_separator}A.28.dmi" "${ok_path}${dir_separator}confusing.dmi" "${error_path}" )

macro( parserTest dmi_file )
  get_filename_component( j ${dmi_file} NAME )          # ${dmi_file} includes the relative path
  string(REGEX REPLACE "\..*$" "" k ${j} )              # ${k} is now the filename minus extension

  #some files produce errors or warnings and must be handled differently. the following foreach
  #compares the file path to the files/paths in ${files_not_clean}. test_type is set accordingly
  set( test_type "ok" )
  foreach( f ${files_not_clean} )
    if( dmi_file MATCHES "${f}.*" )
      set( test_type "out" )     #the test will behave as runOut does in the testFullParser script
      break()
    endif()
  endforeach()

  add_test( NAME ${k}
            COMMAND ${CMAKE_COMMAND}
            -DEXEC_PATH=${exe_path}
            -DDMI=${dmi_file}
            -DOUT_PATH=${pars_test_path}
            -DTTYPE=${test_type}
            -P ${CMAKE_CURRENT_SOURCE_DIR}/checkParser.cmake
          )
endmacro()


#-----------------------------------------------------------------------
#functionality of testFullTester
